﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DeviceFinder.Models;

namespace DeviceFinder.DAL
{
    public class CommandDAL : BaseDAL
    {
        public IQueryable<COMMAND> GetAllCommandByDeviceID(int DeviceID)
        {
            var lst = from command in entities.COMMANDs
                      where command.IDDEVICE == DeviceID
                      select command;
            return lst;
        }

        public IQueryable<COMMAND> GetAllCommandByDeviceIDUndone(int DeviceID)
        {
            var lst = from command in entities.COMMANDs
                      where command.IDDEVICE == DeviceID && command.STATUS == "undone"
                      select command;
            return lst;
        }

        public COMMAND GetCommandByID(int ID)
        {
            COMMAND result = (from command in entities.COMMANDs
                              where command.ID == ID
                              select command).First();

            return result;
        }

        public bool AddCommand(COMMAND cm)
        {
            entities.COMMANDs.Add(cm);
            if (entities.SaveChanges() > 0)
                return true;
            return false;
        }

        public bool EditCommand(COMMAND cm)
        {
            var eCommand = (from command in entities.COMMANDs
                            where command.ID == cm.ID
                            select command).First();

            if (eCommand != null)
            {
                eCommand.DATE = cm.DATE;
                eCommand.DEVICE = cm.DEVICE;
                eCommand.IDDEVICE = cm.IDDEVICE;
                eCommand.OPCODE = cm.OPCODE;
                eCommand.PARAM = cm.PARAM;
                eCommand.STATUS = cm.STATUS;
                try
                {
                    if (entities.SaveChanges() > 0)
                        return true;
                }
                catch (System.Exception e) { }
            }

            return false;
        }

        public bool DeleteCommand(int ID)
        {
            COMMAND dCommand = (from command in entities.COMMANDs
                                where command.ID == ID
                                select command).First();

            entities.COMMANDs.Remove(dCommand);
            if (entities.SaveChanges() > 0)
                return true;

            return false;
        }

        [Flags]
        public enum COMMANDSTATUS
        {
            DONE = 1,
            UNDONE = 0,
            ERROR = -1
        }

        public enum OPCODETYPE
        {
            SHELL = 1,
            CLIENT_BACKUP = 2,
            CLIENT_WEBCAM_CAPTURE = 3
        }
    }
}